package cn.yours.web.controller;

import cn.yours.web.pojo.dto.AdminDTO;
import cn.yours.web.pojo.dto.AdminPageQueryDTO;
import cn.yours.web.pojo.entity.Admin;
import cn.yours.web.result.PageResult;
import cn.yours.web.result.Result;
import cn.yours.web.service.IAdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 管理员Controller
 * 
 * @author ruoyi
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController
{
    @Autowired
    private IAdminService adminService;

    @PostMapping("/list")
    public Result<PageResult> list(@RequestBody AdminPageQueryDTO adminPageQueryDTO) {
        log.info("查询管理员列表:{}",adminPageQueryDTO);
        PageResult pageResult = adminService.selectAdminList(adminPageQueryDTO);
        log.info("pageResult:{}",pageResult);
        return Result.success("查询管理员数据成功", pageResult);

    }

    /**
     * 获取管理员详细信息
     */
    @GetMapping(value = "/{id}")
    public Result<Admin> getInfo(@PathVariable("id") Long id)
    {
        return Result.success("管理员详细信息查询成功",adminService.selectAdminById(id));
    }

    /**
     * 新增管理员
     */
    @PostMapping
    public Result<Void> add(@RequestBody Admin admin)
    {
        System.out.println(admin);
        int i = adminService.insertAdmin(admin);
        if (i!=0){
            return Result.success();
        }
        return Result.error(1000,"新增数据失败");
    }

    /**
     * 修改管理员
     */
    @PutMapping
    public Result<Void> edit(@RequestBody Admin admin)
    {
        System.out.println(admin);
        int i = adminService.updateAdmin(admin);
        if (i==1){
            return Result.success();
        }
        return Result.error(1001,"修改失败");
    }

    /**
     * 删除管理员
     */
	@DeleteMapping("/{ids}")
    public Result<Void> remove(@PathVariable Long[] ids)
    {
        int i = adminService.deleteAdminByIds(ids);
        if (i!= 0){
            return Result.success();
        }
        return Result.error(1002,"删除失败");
    }
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody AdminDTO adminDTO) {
        log.info("发起登录请求");
        String token = adminService.login(adminDTO);
        return Result.success("登录成功",token);
    }
    //注册
    @PostMapping("/enroll")
    public Result enroll(@RequestBody AdminDTO adminDTO) {
        int mark = adminService.enroll(adminDTO);
        if (mark == 0){
            return Result.error(10011,"注册失败");
        }
        return Result.success();
    }
    //忘记密码
    @PostMapping("/forget")
    public Result forget(@RequestBody AdminDTO adminDTO) {
        System.out.println(adminDTO);
        adminService.forget(adminDTO);
        return Result.success("密码重置成功",null);
    }
    //修改密码
    @PutMapping("/updatePwd")
    public Result updatePwd(@RequestBody AdminDTO adminDTO){
        log.info("{}",adminDTO);
        adminService.updatePwd(adminDTO);
        return Result.success("修改密码成功",null);
    }
}
